package com.company;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

import jxl.*;
import jxl.read.biff.BiffException;

/**
 * Created by Administrator on 2016.8.22.
 */
public class Reduce {
    private ArrayList<ArrayList<Double>> arrayLists;

    //进行数据的导入
    public Reduce(String address, int sheetNumber) throws IOException, BiffException {
        arrayLists = new ArrayList<>();

        Workbook workbook = Workbook.getWorkbook(new File(address));

        Sheet sheet = workbook.getSheet(sheetNumber);

        for (int i = 0; i < sheet.getRows(); i++) {
            ArrayList<Double> row = new ArrayList<>();

            for (int j = 0; j < sheet.getRow(i).length; j++) {
                try {
                    row.add(Double.valueOf(sheet.getCell(j, i).getContents()));
                }catch (Exception e){

                }
            }

            arrayLists.add(row);
        }

    }

    //秩序处理
    public ArrayList<ArrayList<Double>> orderProduce(){
        ArrayList<ArrayList<Double>> orderArrayLists=new ArrayList<>();
        ArrayList<ArrayList<Double>> arrayLists=new ArrayList<>();

        for (int i=0;i<this.arrayLists.get(0).size();i++){
            arrayLists.add(new ArrayList<Double>());
            for (int j=0;j<this.arrayLists.size();j++){
                arrayLists.get(i).add(0.0);
            }
        }

        for (int i=0;i<this.arrayLists.size();i++){
            for (int j=0;j<this.arrayLists.get(i).size();j++){
                arrayLists.get(j).set(i,this.arrayLists.get(i).get(j));
            }
        }

        //秩序化
        for (int k=0;k<arrayLists.size();k++){
            ArrayList<Double> numbers=arrayLists.get(k);

            int[] sign=new int[numbers.size()];

            for(int i=0;i<sign.length;i++){
                sign[i]=i;
            }

            for (int i=0;i<numbers.size();i++){
                for(int j=0;j<numbers.size()-1;j++) {
                    if(numbers.get(j)>numbers.get(j+1)){
                        double z=numbers.get(j);
                        numbers.set(j,numbers.get(j+1));
                        numbers.set(j+1,z);

                        int t=sign[j];
                        sign[j]=sign[j+1];
                        sign[j+1]=t;
                    }
                }
            }

            ArrayList<Double> z=new ArrayList<>();
            for (int i=0;i<numbers.size();i++){
                z.add(i+0.00000);
            }
            for (int i=0;i<sign.length;i++){
                z.set(sign[i],i+1.000000);
            }
            orderArrayLists.add(z);
        }

        Stack<Double> stack=new Stack<>();
        Stack<Integer> signStack=new Stack<>();

        //用栈来合并相同的项
        for (int k=0;k<arrayLists.size();k++){
            ArrayList<Double> numbers=arrayLists.get(k);

            for(int i=0;i<numbers.size();i++){
                if(stack.isEmpty()||numbers.get(i).equals(stack.peek())){
                    stack.push(numbers.get(i));
                    signStack.push(i+1);
                }else if(stack.size()==1){
                    stack.pop();
                    signStack.pop();
                    stack.push(numbers.get(i));
                    signStack.push(i+1);
                }else {
                    ArrayList<Double> z=new ArrayList<>();
                    double average=0;
                    int size=0;
                    while (!stack.isEmpty()){
                        average+=signStack.peek();
                        z.add(Double.valueOf(signStack.pop()));
                        stack.pop();
                        size++;
                    }
                    average=average/size;

                    for (int j=0;j<orderArrayLists.get(k).size();j++){
                        for (Double d:z){
                            if(orderArrayLists.get(k).get(j).equals(d)){
                                orderArrayLists.get(k).set(j,average);
                                break;
                            }
                        }
                    }
                    stack.push(numbers.get(i));
                    signStack.push(i+1);
                }
            }
        }

        return orderArrayLists;
    }
}
